package com.molichuxing.gateway.api.services.impl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.molichuxing.gateway.bff.user.entity.response.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseCodeEnum;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.framework.utils.IpUtil;
import com.molichuxing.gateway.api.services.UserApiService;
import com.molichuxing.gateway.bff.user.entity.request.create.ComplaintCreateVo;
import com.molichuxing.gateway.bff.user.entity.request.create.UserCreateVo;
import com.molichuxing.gateway.bff.user.entity.request.modify.UserModifyVo;
import com.molichuxing.gateway.bff.user.services.UserBffService;
import com.molichuxing.gateway.utils.redis.TokenUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * 
 * @author liyg
 * 用户网关接口实现
 * 2019-09-18
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping(value = "/user")
public class UserApiServiceImpl extends RootApiService implements UserApiService{

	private static final Logger logger = LoggerFactory.getLogger(UserApiServiceImpl.class);
	
	@Resource
	private TokenUtil tokenUtil;

	@Resource
	private UserBffService userBffService;
	
	
	/**
	 * 用户登录注册接口
	 * @param createVo
	 * @param request
	 * @return
	 */
	@Override
	@ApiOperation(value = "用户登录注册接口")
	@PostMapping(value = "/login")
	public ResponseResult<UserLoginVo> login(@RequestBody UserCreateVo createVo,HttpServletRequest request,@RequestHeader(value = "openId", required = false) String openId){
		ResponseResult<UserLoginVo>  result = new ResponseResult<>();
		UserLoginVo vo=new UserLoginVo();
		result.setData(vo);
		try {
			String ipAddr = IpUtil.getIpAddr(request);
			createVo.setIpAddr(ipAddr);
			createVo.setOpenId(openId);
			return userBffService.memberUserLogin(createVo);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[login]用户登录注册接口异常", e);
			result.error("用户登录接口异常");
		}
		return result;
	}
	
	/**
	 * 用户退出接口
	 * @param token
	 * @param channel
	 * @return
	 */
	@Override
	@ApiOperation(value = "用户登出接口")
	@PostMapping(value = "/logOut")
	public ResponseResult<Boolean> logOut(@RequestHeader(value = "token", required = false) String token,
			@RequestHeader(value = "channel", required = true) String channel){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			 Boolean data = userBffService.logOut(token,channel);
			 return result.success(data);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[logOut用户退出接口异常", e);
			result.error("用户退出接口异常");
		}
		return result;
	}

	/**
	 * 根据token获取用户信息
	 * @param token
	 * @return
	 */
	@Override
	@ApiOperation(value = "根据token获取用户信息")
	@GetMapping(value = "/detail")
	public ResponseResult<UserVo> getUser(@RequestHeader(value = "token", required = false) String token){
		ResponseResult<UserVo>  result = new ResponseResult<>();
		try {
			 UserVo userVo = userBffService.getUserByToken(token);
			 return result.success(userVo);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getUser]根据token获取用户信息异常", e);
			result.error("根据token获取用户信息异常");
		}
		return result;
	}
	
	/**
	 * 修改用户信息接口
	 * @param modifyVo
	 * @param token
	 * @return
	 */
	@Override
	@ApiOperation(value = "修改用户信息接口(个人信息页)")
	@PostMapping(value = "/modify")
	public ResponseResult<String> modifyUser(@RequestBody UserModifyVo modifyVo,@RequestHeader(value = "token", required = false) String token){
		ResponseResult<String>  result = new ResponseResult<>();
		try {
			return userBffService.modifyUser(modifyVo,token);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[modifyUser]修改用户信息接口异常", e);
			result.error("修改用户信息接口异常",null);
		}
		return result;
	}
	
	/**
	 * 修改用户地址接口
	 * @param modifyVo
	 * @param token
	 * @return
	 */
	@Override
	@ApiOperation(value = "修改用户地址接口")
	@PostMapping(value = "/modify/address")
	public ResponseResult<Boolean> modifyUserAddress(@RequestBody UserModifyVo modifyVo,@RequestHeader(value = "token", required = false) String token){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			 Boolean data = userBffService.modifyUserAddress(modifyVo,token);
			 return result.success(data);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[modifyUserAddress]修改用户地址接口异常", e);
			result.error("修改用户地址接口",null);
		}
		return result;
	}

	/**
	 * 获取用户驾驶证信息
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取用户驾驶证信息")
	@PostMapping(value = "/drivingLicense")
	public ResponseResult<DrivingLicenseVo> getDrivingLicense(){
		ResponseResult<DrivingLicenseVo>  result = new ResponseResult<>();
		try {
			 DrivingLicenseVo drivingLicense = userBffService.getDrivingLicense();
			 return result.success(drivingLicense);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getDrivingLicense]获取用户驾驶证信息接口异常", e);
			result.error("获取用户驾驶证信息地址接口",null);
		}
		return result;
	}
	
	
	/**
	 * 获取用户投诉信息
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取用户投诉信息")
	@GetMapping(value = "/complaint/detail")
	public ResponseResult<ComplaintVo> getComplaintVo(){
		ResponseResult<ComplaintVo> result = new ResponseResult<>();
		try {
			ComplaintVo complaintVo = userBffService.getComplaintVo();
			return result.success(complaintVo);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getComplaintVo]获取用户投诉信息接口异常", e);
			result.error("获取用户投诉信息地址接口",null);
		}
		return result;
	}

	/**
	 * 提交用户投诉信息
	 * @param createVo
	 * @return
	 */
	@Override
	@ApiOperation(value = "提交用户投诉信息")
	@PostMapping(value = "/complaint/create")
	public ResponseResult<Boolean> createComplaint(@RequestBody ComplaintCreateVo createVo){
		ResponseResult<Boolean> result = new ResponseResult<>();
		try {
			Boolean flag = userBffService.createComplaint(createVo);
			if(!flag) {
				result.error("提交用户投诉信息失败",false);
			}
			return result.success(flag);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[createComplaint]提交用户投诉信息接口异常", e);
			result.error("提交用户投诉信息地址接口",null);
		}
		return result;
	}
	
	@Override
	@ApiOperation(value = "查询消息列表")
	@GetMapping(value = "/message/list")
	public ResponseResult<Paged<MessageCenterVo>> getMessageCenterPage(@RequestParam Integer pageNum, @RequestParam  Integer pageSize) {
		ResponseResult<Paged<MessageCenterVo>> result = new ResponseResult<>();
		try {
			return result.success(userBffService.getMessageCenterPage(pageNum,pageSize));
		} catch (Exception e) {
			logger.error("查询消息列表异常", e);
			result.error("查询消息列表异常");
		}
		return result;
	}


	@Override
	@ApiOperation(value = "查询用户未读消息数量")
	@GetMapping(value = "/message/count")
	public ResponseResult<Integer> getUnreadCount() {
		ResponseResult<Integer> result = new ResponseResult<>();
		try {
			return result.success(userBffService.getUnreadCount());
		} catch (Exception e) {
			logger.error("查询用户未读消息数量异常", e);
			result.error("查询用户未读消息数量异常");
		}
		return result;
	}

	@Override
	@ApiOperation(value = "检验用户是否黑名单和下单信息")
	@GetMapping(value = "/verify")
	public ResponseResult<UserVerifyVo> verify(@RequestHeader(value = "token", required = false) String token,
			@RequestHeader(value = "deviceNum", required = true) String deviceNum) {
		ResponseResult<UserVerifyVo> result = new ResponseResult<>();
		try {
			// 根据token获取用户信息
			UserVo user = tokenUtil.getUser(token);
			// 验证
			result.success(userBffService.getVerify(user, deviceNum));

		} catch (BizException e) {
			logger.error("[verify]询检验用户是否黑名单和下单信息异常:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[verify]查询检验用户是否黑名单和下单信息异常:", e);
			result.error("查询异常");
		}
		return result;
	}

	@Override
	@ApiOperation(value = "根据token判断用户跳转的首页.1:我的爱车，0：首页")
	@GetMapping(value = "/verify/home/page")
	public ResponseResult<Boolean> verifyUserToIndex(@RequestHeader(value = "token", required = false) String token) {
		ResponseResult<Boolean> result = new ResponseResult<>();
		try {
			// 根据token获取用户信息
			UserVo user = tokenUtil.getUser(token);
			if(null == user) {
				return result.success(false);
			}
			result.success(userBffService.verifyUserToIndex(user.getUserId()));
		} catch (BizException e) {
			logger.error("[verify]根据token判断用户跳转的首页异常:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[verify]根据token判断用户跳转的首页异常:", e);
			result.error("查询异常");
		}
		return result;
	}


	@Override
	@ApiOperation(value = "根据token获取我的爱车信息")
	@GetMapping(value = "/home/my/car")
	public ResponseResult<List<UserHomeMyCarVo>> getHomeMyCar(@RequestHeader(value = "token", required = false) String token,
															  @RequestHeader(value = "channel", required = false) String channel) {
		ResponseResult<List<UserHomeMyCarVo>> result = new ResponseResult<>();
		try {
			// 根据token获取用户信息
			UserVo user = tokenUtil.getUser(token);
			result.success(userBffService.getHomeMyCar(user.getUserId(), channel));
		} catch (BizException e) {
			logger.error("[verify]根据token获取我的爱车信息异常:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[verify]根据token获取我的爱车信息异常:", e);
			result.error("查询异常");
		}
		return result;
	}

}
